using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage; 
using Demo;
using System;
using System.Threading;

namespace GameEngine
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Engine : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

        private static Engine inst;
        public InputEngine user;
        public Camera camera;
       public Scene beach;
       static float time = 0f;



        
        public Engine()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 640;
            graphics.PreferredBackBufferHeight = 480;
            Content.RootDirectory = "Content";
        }

        public static float Time
        {
            get { return time; }
        }
        protected override void Initialize()
        {
            inst = this;
            Window.Title = "PPWater";
            base.Initialize();
        }

        protected override void LoadContent()
        {
            Render.Initialize(this);

            beach = new Scene(this,null);
            camera = new Camera(new Vector3(0, 3, 10), 0, 0, 10, this);
            
            beach.Initialize();
            
            Components.Add(camera);
            Components.Add(beach);
        }

        protected override void UnloadContent()
        {
            Render.Release();
        }
        protected override void Update(GameTime gameTime)
        {
            InputEngine.Update(gameTime);
            
            time += gameTime.ElapsedGameTime.Milliseconds / 1000f;



            if (InputEngine.IsKeyDown(Keys.X))
            {

                foreach (IGameComponent component in Components)
                {
                    if (component is EngineEntity)
                    {
                        EngineEntity entity = component as EngineEntity;
                        entity.ReloadGraphics();
                    }
                }
            }  
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; 

            if(InputEngine.IsLeftMouseDown())
            {
                camera.MoveForward(dt);
            }

            if(InputEngine.IsRightMouseDown())
            {
                camera.MoveForward(-dt);
            }


            base.Update(gameTime);
        }

        public static Engine GetInstance
        {
            get { return inst; }
        }
        public static void DrawScene() {

            inst.DrawScene(null);
    }

        public void DrawScene(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            Render.UpdateShaderParameters();

           
            Render.RenderScene();
            Render.RenderDeferred();
            Render.RenderPostProcess();
            Thread.Sleep(61);
        }
    }
}
